package com.zzh.partnersys.service.impl;

import com.zzh.partnersys.common.exception.BusinessException;
import com.zzh.partnersys.common.exception.ResultCodeEnum;
import com.zzh.partnersys.entity.UserFriendDO;
import com.zzh.partnersys.entity.vo.UserFriendVO;
import com.zzh.partnersys.mapper.UserFriendMapper;
import com.zzh.partnersys.service.UserFriendService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zzh.partnersys.utils.FriendKeyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * 用户联系人表 服务实现�?
 * </p>
 *
 * @author zzh
 * @since 2024-08-11
 */
@Service
public class UserFriendServiceImpl extends ServiceImpl<UserFriendMapper, UserFriendDO> implements UserFriendService {
    @Autowired
    private UserFriendMapper userFriendMapper;

    @Override
    @Transactional
    public boolean insertUserFriend(Long fromUid, Long toUid,String fromName,String toName) {
        String friendKeyUID = FriendKeyUtils.getFriendKeyUID(fromUid, toUid);
        UserFriendDO userFriendDO = new UserFriendDO();
        userFriendDO.setUid(fromUid);
        userFriendDO.setFriendUid(toUid);
        userFriendDO.setName(fromName);
        userFriendDO.setFriendKey(friendKeyUID);//好友间的唯一标识

        UserFriendDO userFriendDO1 = new UserFriendDO();
        userFriendDO1.setUid(toUid);
        userFriendDO1.setFriendUid(fromUid);
        userFriendDO1.setName(toName);
        userFriendDO1.setFriendKey(friendKeyUID);//好友间的唯一标识

        int insert1 = userFriendMapper.insert(userFriendDO);
        int insert2 = userFriendMapper.insert(userFriendDO1);
        if (insert1 + insert2 == 2){
            return true;
        }
        return false;
    }

    /**
     * 查询用户的好友列�?
     * @param userId
     * @return
     */
    @Override
    public List<UserFriendVO> listFriend(Long userId) {
        if (userId == null){
            throw new BusinessException(ResultCodeEnum.PARAMS_ERROR);
        }
        List<UserFriendVO> userFriendVOList = userFriendMapper.listFriend(userId);
        return userFriendVOList;
    }
}
